package com.uenpay.oem.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.uenpay.oem.R;
import com.uenpay.oem.widget.view.VerificationCodeInput;
import com.uenpay.smallvideo.util.Log;



/**
 * Created by PS on 2017/6/18.
 */

public class ReserverodeDialog extends Dialog {
    private String message;
	private ImageView close_iv;
	private String phone;
	private TextView tvAmount;
	private TextView tvPhone;
	private String amount;
	private VerificationCodeInput message_code;
	private onMessageListenner onMessageclickListener;
	private View llNoReceiveCode;
	private View llTimeLeft;
	private TextView tvTimeLeft;

	public ReserverodeDialog(Context context,String phone,String amount) {
		super(context, R.style.dialog_custom);
		this.phone = phone;
		this.amount = amount;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_securitycode);
		Window window = getWindow();
		window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置为居中
		// 按空白处不能取消动画
		setCanceledOnTouchOutside(false);
		// 初始化控件
		initview();
		// 初始化界面控件的事件
		initEvent();
	}

	private void initEvent() {
		close_iv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		message_code.setOnCompleteListener(new VerificationCodeInput.Listener() {
            @Override
			public void onComplete(String content) {
					if (onMessageclickListener!=null){
						onMessageclickListener.onMessageclick(content);
					}

			}
		});
		//重新发送短信验证码
		llNoReceiveCode.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (resendListener != null) {
					resendListener.resendSms();
				}
			}
		});
	}


	private void initview() {
		// 点击关闭
		close_iv = (ImageView) findViewById(R.id.dialog_close_code);
		// 验证码
		message_code = (VerificationCodeInput) findViewById(R.id.message_code);
		llNoReceiveCode = findViewById(R.id.ll_no_receive_code);
		llNoReceiveCode.setVisibility(View.GONE);
		llTimeLeft = findViewById(R.id.ll_time_left);
		tvTimeLeft = (TextView) findViewById(R.id.tv_time_left);
		tvAmount = (TextView) findViewById(R.id.tv_amount);
		tvPhone = (TextView) findViewById(R.id.tv_send_to_phone);
		tvAmount.setText("￥"+amount);
		tvPhone.setText("验证码已发送至   "+phone);

	}

	/**
	 * 短信验证码
	 */
	public void setOnMessageListenner(onMessageListenner onMessageListenner) {
		this.onMessageclickListener=onMessageListenner;

	}

	public interface onMessageListenner {
		void onMessageclick(String message);
	}

	/**
	 * 设置倒计时文本
	 * @param timeLeft 剩余时间
	 */
	public void setTimeLeft(int timeLeft){
		tvTimeLeft.setText(""+timeLeft+" s");
	}

	/**
	 *  设置对话框倒计时的text
	 * @param isCounting 是否倒计时
	 */
	public void setViewVisible(boolean isCounting){
		if(isCounting){
			llTimeLeft.setVisibility(View.VISIBLE);
			llNoReceiveCode.setVisibility(View.GONE);
		} else {
			llTimeLeft.setVisibility(View.GONE);
			llNoReceiveCode.setVisibility(View.VISIBLE);
		}
	}
	//重新发送短信的接口
	public interface ResendListener{
		void resendSms();
	}

	private ResendListener resendListener;

	public void setOnResendListener(ResendListener listener) {
		resendListener = listener;
	}
}
